﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace BLL.Models
{
    
    public class MBrandMModels
    {
        public string[] str_files = { "ID", "Name", "Content", "Img_Logo", "Img_LogoB", "Intro" };
        /// <summary>
        /// 添加品牌
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddBrand(MODEL.MBrand model) {

            bool ret = false;
            string str_sql = "insert into [tb_mc_brand] ( [Name], Content, Img_Logo, Img_LogoB, Intro)values( '"+model.Name+"','"+model.Content+"' ,'"+model.Img_Logo+"' ,'"+model.Img_LogoB+"','"+model.Intro+"' )";
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            return ret;
        }
        /// <summary>
        /// 修改品牌
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateBrand(MODEL.MBrand model) {

            bool ret = false;
            string str_sql = "update [tb_mc_brand] set Name='"+model.Name+"', Content='"+model.Content+"', Img_Logo='"+model.Img_Logo+"', Img_LogoB='"+model.Img_LogoB+"', Intro='"+model.Intro+"' where id= "+model.Id.ToString();

            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            return ret;
        }
        /// <summary>
        /// 删除品牌
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelBrand(string id) {

            bool ret = false;
            string str_sql = " delete from [tb_mc_brand] where id in("+id+") ";
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            return ret;
        }
        /// <summary>
        /// 品牌分类
        /// </summary>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <param name="fiter"></param>
        /// <returns></returns>
        public string AllBrand(string start,string limit,string fiter) {

            string ret = string.Empty;
            string str_sql = "select top "+limit+" * from [tb_mc_brand] where id not in( select "+start+" id from [tb_mc_brand] order by id desc ) order by id desc";
            if(start=="0"){

                str_sql = " select top " + limit + " * from [tb_mc_brand] order by id desc ";
            }
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            DataTable dt = new DataTable();
            string tcount = "0";
            try
            {
               dt= db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
               tcount = db.ExecuteScalar(COMM.PucConfig.ConnectionStringA, CommandType.Text, "select count(*) from [tb_mc_brand] ").ToString();
            }
            catch (Exception ex) { }

            BLL.JSONHelper jsons = new BLL.JSONHelper();



            foreach (DataRow dr in dt.Rows)
            {

                foreach (string s in str_files)
                {

                    jsons.AddItem(s, dr[s].ToString());
                }
                jsons.ItemOk();

            }


            jsons.totlalCount = Convert.ToInt32(tcount);
            ret = jsons.ToString();
            return ret;
        }
        /// <summary>
        ///获取一条品牌信息 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetBrandById(string id) {

            string ret = string.Empty;
            string str_sql = "select * from [tb_mc_brand] where id="+id;
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            DataTable dt = new DataTable();
            try
            {
               dt=db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
            }
            catch (Exception ex) { }
            BLL.FormJSONHelper jsons = new BLL.FormJSONHelper();
            jsons.success = true;


            foreach (DataRow dr in dt.Rows)
            {

                foreach (string s in str_files)
                {

                    jsons.AddItem(s, dr[s].ToString());
                }
                jsons.ItemOk();

            }



            ret = jsons.ToString();
            return ret;
        }
        /// <summary>
        ///获得品牌信息
        /// </summary>
        /// <returns></returns>
        public string GetInfoProductinfo()
        {

            string ret = string.Empty;
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            DataTable dt = new DataTable();

            dt = GetBrabdByPidAndFiter(" 1=1 ");


            BLL.JSONHelper jsons = new BLL.JSONHelper();


            jsons.AddItem("ID", "0");
            jsons.AddItem("ClassName", "不指定品牌");
            jsons.ItemOk();

            foreach(DataRow dr in dt.Rows){

                jsons.AddItem("ID", dr["id"].ToString());
                jsons.AddItem("ClassName", dr["name"].ToString());
                jsons.ItemOk();

            }
            ret = jsons.ToString();
            return ret;
        }
        /// <summary>
        /// 根据条件获取品牌信息
        /// </summary>
        /// <param name="fiter"></param>
        /// <returns></returns>
        public DataTable GetBrabdByPidAndFiter(string fiter)
        {

            string fiternew = " 1=1 and " + fiter ;
            string ret = string.Empty;
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            string str_sql = "  select  * from [tb_mc_brand] where " + fiternew + "  order by id   ";
            DataTable dt = new DataTable();
            try
            {

                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
            }
            catch (Exception ex) { }
            return dt;
        }
    }
}
